//
//  WJMoreSelectView.m
//  WJSelectedItemsDemo
//
//  Created by 王杰 on 2019/7/22.
//  Copyright © 2019 王杰. All rights reserved.
//
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#import "WJMoreSelectView.h"
#import "WJLiveCourseCollectionItem.h"
#import "WJLiveCourseCollectionHeader.h"
#import "WJLiveCourseCollectionFooter.h"

@interface WJMoreSelectView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,assign)CGFloat viewHeight;
@property (nonatomic,strong)WJLiveCourseCollectionFooter * fooetrView;
@property (nonatomic,strong)NSMutableArray *selectedDataArray;

@end

@implementation WJMoreSelectView
static NSString * const reuseIdentifier = @"Cell";


-(NSMutableArray *)selectedDataArray{
    if (!_selectedDataArray) {
        _selectedDataArray = [[NSMutableArray alloc]init];
    }
    return _selectedDataArray;
}

- (instancetype)initWithFrame:(CGRect)frame
         collectionViewLayout:(UICollectionViewLayout *)layout{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self createUI];
    }
    return self;
}

// 创建界面
- (void)createUI{
    self.backgroundColor = UIColor.whiteColor;
    self.delegate = self;
    self.dataSource = self;
    self.alwaysBounceVertical = YES;
    self.allowsMultipleSelection = NO; //支持同时选中多行 不走 didDeselectItemAtIndexPath 代理
    [self registerClass:[WJLiveCourseCollectionItem class] forCellWithReuseIdentifier:reuseIdentifier];
    [self registerClass:[WJLiveCourseCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HEADER"];
    [self registerClass:[WJLiveCourseCollectionFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FOOTER"];
    [self addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"itemTitle" options:NSKeyValueObservingOptionNew context:nil];
    
}


#pragma mark - KVO 监听
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"contentSize"]) {
        
        _viewHeight = [self contentSize].height;
        CGRect newFrame = self.frame;
        newFrame.size.height = _viewHeight;
        self.frame = newFrame;
    }
    
    if ([keyPath isEqualToString:@"itemTitle"]) {
        self.fooetrView.priceLabel.text = self.itemTitle;
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WJLiveCourseCollectionItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    WJLiveCourseItemModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WJLiveCourseItemModel * model = self.dataArray[indexPath.item];
    model.isSel = !model.isSel;
    if (model.isSel ==YES) {
        [self.selectedDataArray addObject:model];
    } else {
        [self.selectedDataArray removeObject:model];
    }
    [self reloadData];
    if (self.changeItemsdBlock) {
        self.changeItemsdBlock(self.selectedDataArray, YES);
    }
}


#pragma mark - 全选
-(void)selectAllForIcon{
    [self.selectedDataArray removeAllObjects];
    for (int i =0; i<self.dataArray.count; i++) {
        self.dataArray[i].isSel = YES;
        [self.selectedDataArray addObject:self.dataArray[i]];
        [self reloadData];
        if (self.changeItemsdBlock) {
            self.changeItemsdBlock(self.selectedDataArray, YES);
        }
    }
}

#pragma mark - 取消全选
-(void)deselectAllForIcon{
    for (int i =0; i<self.dataArray.count; i++) {
        self.dataArray[i].isSel = NO;
        [self.selectedDataArray removeAllObjects];
    }
    [self reloadData];
    if (self.changeItemsdBlock) {
        self.changeItemsdBlock(self.selectedDataArray, NO);
    }
}


- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        WJLiveCourseCollectionHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HEADER" forIndexPath:indexPath];
        headerView.titleLabel.text = self.title;
        headerView.bottomLabel.text = @"按阶段购买";
        
        [headerView setConfirmBlock:^{
            [self selectAllForIcon];
        }];
        
        [headerView setCancelBlock:^{
            [self deselectAllForIcon];
        }];        
        
        if (self.dataArray.count == self.selectedDataArray.count ) {
            [headerView.allButton setSelected:YES];
        }else{
            [headerView.allButton setSelected:NO];
        }
        
        return headerView;
    }else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
        self.fooetrView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FOOTER" forIndexPath:indexPath];
        self.fooetrView.priceLabel.text = self.itemTitle;
        return self.fooetrView;
    }else{
        return [[UICollectionReusableView alloc]init];
    }

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenW, 55);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(kScreenW, 80);
}


@end
